package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.redis.CacheService;
import com.heima.feign.wemedia.IWemediaClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApFanMapper;
import com.heima.user.mapper.ApFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserRelationService;
import com.heima.utils.thread.ApUserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRelationServiceImpl extends ServiceImpl<ApFollowMapper, ApUserFollow> implements ApUserRelationService {

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private ApFanMapper apFanMapper;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private IWemediaClient wemediaClient;

    /**
     * 用户关注/取消关注
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //1 参数校验
        if (dto.getOperation() == null || dto.getOperation() < 0 || dto.getOperation() > 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 判断是否登录
        Integer apUserId = ApUserThreadLocalUtil.get();
        if (apUserId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //3 关注 apuser:follow:  apuser:fans:
        Integer followUserId = dto.getAuthorId();
        if (dto.getOperation() == 0) {
            // 将对方写入我的关注中
            cacheService.zAdd(BehaviorConstants.APUSER_FOLLOW_RELATION
                    + apUserId, followUserId.toString(), System.currentTimeMillis());
            // 将我写入对方的粉丝中
            cacheService.zAdd(BehaviorConstants.APUSER_FANS_RELATION
                    + followUserId, apUserId.toString(), System.currentTimeMillis());

            //保存到follow表
            Integer authorId = dto.getAuthorId();
            WmUser wmUser = wemediaClient.getWmUser(authorId);
            String authorName = wmUser.getName();//作者名字
            ApUserFollow apUserFollow = ApUserFollow.builder()
                    .followId(dto.getAuthorId())
                    .userId(ApUserThreadLocalUtil.get())
                    .followName(authorName)
                    .createdTime(new Date())
                    .build();
            save(apUserFollow);

            //保存到fan表
            ApUser fanApUser = apUserMapper.selectById(ApUserThreadLocalUtil.get());
            ApUserFan apUserFan = ApUserFan.builder()
                    .fansId(ApUserThreadLocalUtil.get())
                    .userId(dto.getAuthorId())
                    .fansName(fanApUser.getName())
                    .createdTime(new Date())
                    .build();
            apFanMapper.insert(apUserFan);


        } else {
            //直接删除
            remove(Wrappers.<ApUserFollow>lambdaQuery()
                    .eq(ApUserFollow::getUserId,ApUserThreadLocalUtil.get())
                    .eq(ApUserFollow::getFollowId,dto.getAuthorId()));
            apFanMapper.delete(Wrappers.<ApUserFan>lambdaQuery()
                    .eq(ApUserFan::getFansId,ApUserThreadLocalUtil.get())
                    .eq(ApUserFan::getUserId,dto.getAuthorId()));
            // 取消关注
            cacheService.zRemove(BehaviorConstants.APUSER_FOLLOW_RELATION + apUserId, followUserId.toString());
            cacheService.zRemove(BehaviorConstants.APUSER_FANS_RELATION + followUserId, apUserId.toString());
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }
}